package ${package.ServiceImpl};

import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import ${superServiceImplClassPackage};
import org.springframework.stereotype.Service;
import ${package.Entity}.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import ${package.Parent}.utils.WebUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ${package.Entity}.PageList;
import org.apache.commons.lang.StringUtils;
import java.util.Arrays;
import ${package.Other}.${table.entityPath}.DeleDTO;
import ${package.Other}.${table.entityPath}.ListDTO;
/**
 * <p>
 * ${table.comment!} 服务实现类
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@Service
<#if kotlin>
open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} {

}
<#else>
public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} {

    @Autowired
    private ${table.entityName}Mapper ${table.entityPath}Mapper;

   @Override
   public ResponseResult listPage(ListDTO listDTO) {
        IPage iPage=new Page(listDTO.getPage(),listDTO.getLimit());
        QueryWrapper queryWrapper=new QueryWrapper();
        if (StringUtils.isNotBlank(listDTO.getSearchField()) && StringUtils.isNotBlank(listDTO.getSearchValue())) {
            // TODO
            String[] arr = new String[]{"adminRoleId", "isDisable"};
            if (Arrays.asList(arr).contains(listDTO.getSearchField())) {
                //等于判断
                queryWrapper.eq(WebUtils.humpToLine(listDTO.getSearchField()), listDTO.getSearchValue());
            } else {
            //like判断
                queryWrapper.like(WebUtils.humpToLine(listDTO.getSearchField()), listDTO.getSearchValue());
            }
        }
        if (null!=listDTO.getDateValue() && listDTO.getDateValue().size()!=0) {
              queryWrapper.ge(WebUtils.humpToLine(listDTO.getDateField()),listDTO.getDateValue().get(0)+ " 00:00:00");
              queryWrapper.le(WebUtils.humpToLine(listDTO.getDateField()),listDTO.getDateValue().get(1)+ " 23:59:59");
        }

        if (StringUtils.isNotBlank(listDTO.getSortField())) {
            queryWrapper.orderBy(true,"asc".equals(listDTO.getSortValue()),WebUtils.humpToLine(listDTO.getSortField()));
        }

        IPage data = ${table.entityPath}Mapper.selectPage(iPage, queryWrapper);
        PageList pageList=new PageList();
        pageList.setCount(data.getTotal());
        pageList.setLimit(data.getSize());
        pageList.setList(data.getRecords());
        pageList.setPage(data.getCurrent());
        pageList.setPages(data.getPages());

        return new ResponseResult(pageList);
   }

   @Override
   public ResponseResult dele(DeleDTO deleDTO) {
        UpdateWrapper updateWrapper=new UpdateWrapper();
        updateWrapper.in(WebUtils.humpToLine("${table.fields[0].propertyName}"),deleDTO.getIds());
        ${table.entityPath}Mapper.delete(updateWrapper);
        return new ResponseResult(200,"删除成功");
   }

   @Override
   public ResponseResult add(${table.entityName} ${table.entityPath}) {
        ${table.entityPath}Mapper.insert(${table.entityPath});
        return new ResponseResult(200,"新增成功");
   }

   @Override
   public ResponseResult info(Integer ${table.fields[0].propertyName}) {
        ${table.entityName} ${table.entityPath}= ${table.entityPath}Mapper.selectById(${table.fields[0].propertyName});
        return new ResponseResult(${table.entityPath});
   }

   @Override
   public ResponseResult edit(${table.entityName} ${table.entityPath}) {
        ${table.entityPath}Mapper.updateById(${table.entityPath});
        return new ResponseResult(200,"更新成功");
   }
}
</#if>
